<?php

declare(strict_types=1);

namespace Newbie;


use support\Response;

/**
 * Class NewbieResponse
 * @package MineServer
 */
class NewbieResponse extends Response {
    /**
     * @param string|null $message
     * @param array|object $data
     * @param int $code
     */
    public function success (string $message = null, array|object $data = [], int $code = 200): Response {
        $format = [
            'success' => true,
            'message' => $message ?: t('newbieadmin.response_success'),
            'code'    => $code,
            'data'    => &$data,
        ];
        return json($format);
    }

    /**
     * @param string $message
     * @param int $code
     * @param array $data
     */
    public function error (string $message = '', int $code = 500, array $data = []): Response {
        $format = [
            'success' => false,
            'code'    => $code,
            'message' => $message ?: t('newbieadmin.response_error'),
        ];
        if (!empty($data)) {
            $format['data'] = &$data;
        }
        return json($format);
    }

    /**
     * 向浏览器输出图片
     * @param string $image
     * @param string $type
     * @return Response
     */
    public function responseImage (string $image, string $type = 'image/png'): Response {
        return response()->withHeader('Server', 'MineAdmin')
            ->withHeader('content-type', $type)
            ->withBody($image);
    }

}

